var count = 1;
var base_url = 'http://localhost/kooperacija2013/';
function saveFile() {
    sviRedovi = $('.organogramnovi tbody tr').length;
    style = $("#verzijeload").html();
    console.log(sviRedovi); 
    snimioff();
    //hidegrid();
    $.ajax({
        url: base_url+'verzije/save_version.php',
        type: 'POST',
        data: { style: style,
                sviRedovi: sviRedovi },
        dataType: "json"        
    })
    .done(function(reply) {
        console.log(reply); 
        
        var datumfajl = reply.date;
        //var datum = reply;
		var arrdatum = datumfajl.split('-');
		datum = "<span>" + arrdatum[0] + "." + arrdatum[1]+".</span> - <span>"+arrdatum[3] + ":" + arrdatum[4]+"</span>";
    	ver = '<span class="ver"><b>Verzija-'+count++ +'</b><date>'+datum+'</date><span class="uk-button uk-button-success ucitaj">Ucitaj</span><button class="uk-button uk-button-danger obrisi"  type="button">Obrisi</button><span class="loadverzija">'+datumfajl+'</span></span> ';
		$("#ver").append(ver);
		verNumbers = $('#ver .ver').length;
	    btn = $('.verzije .snimi').get(0);
		
		$('.obrisi').on('click', function(event) {
			event.preventDefault();
			verzijaIme = $(this).parent().find('b').text();
			$(this).parent().remove();
			btn.removeAttribute("disabled","");
			verNumbers = $('#ver .ver').length;
			loadingon();
			brisifajl = $(this).parent().find('.loadverzija').text();
		});
		btnsviobrisi = $('.ver .obrisi');
		filename = datumfajl;
		
		$('.ver .ucitaj').on("click", function(){ 
			imefajla = $(this).parent().find('.loadverzija').html();
			$('.ver').removeClass('ucitano');
			$(this).parent().addClass('ucitano');
			ucitanFajl = $(this).parent().find('b').text();
			btnobrisi = $('.ucitano').find('.obrisi').get(0);
			btnsviobrisi.removeAttr("disabled");
			btnobrisi.setAttribute("disabled","");
			loadingon();
		});
		$('.ver .ucitaj').attr('onclick', 'loadFile()');
		$('.ver .obrisi').attr('onclick', 'deleteFile()');

		verNumbers = $('#ver .ver').length;
		saveVer = $('#ver .ver:last-child').find('b').text();
		
	    if ( verNumbers < 5 ) {
	    	var maxver = 5-verNumbers;
	        console.log('Jos: '+maxver+' Verzija');
	    } else {
	        console.log('Max 5 verzija');
	        btn.setAttribute("disabled","");
	    };
	    loadingoff();
		console.log(saveVer+": "+filename+".html");
    })
    .fail(function(reply) {
        console.log("error");
        console.log(reply); 
    })
    .always(function() {
        console.log("file saved");
    });
}

function loadFile() {
    $.ajax({
        url: base_url+'verzije/load_version.php',
        type: 'POST',
        data: { imefajla: imefajla
         }
    })
    .done(function(reply) {
        console.log(ucitanFajl+': '+imefajla+".html");
        jQuery("#main2").replaceWith(reply);
        
        init_main();
        snimioff();
		editoff();
        init_wordsave();
        $('#edit a').text('Izmeni tekst');
        
        $('.dashed.none, .solid.none').removeClass('none');
		$( "#organogram ul li p.prvobitno.spusteno" ).removeClass('prvobitno');

        init_draggables_and_droppables();

		$( ".spusteno" ).draggable({ 
            revert: "invalid",
            snap:  ".organogramnovi td  > span, #organogram ul li",
            snapMode:'inner',
            snapTolerance: 20
        });
        $( ".spusteno" ).resizable({ 
	        handles: "se",
	        minWidth: 117,
	        minHeight: 40,
	        maxWidth: 283,
	        maxHeight: 118  
	    });
	    loadingoff();
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("file loaded");
    });
}

function deleteFile() {

    $.ajax({
        url: base_url+'verzije/delete_version.php',
        type: 'POST',
        data: { brisifajl: brisifajl  }
    })
    .done(function(reply) {
        loadingoff();
        console.log(verzijaIme+': '+brisifajl+'.html');
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("file deleted");
    });
}
function saveWord1() {
    style = $("#verzijeload").html();
    snimion();

    $.ajax({
        url: base_url+'verzije/save_to_word.php',
        type: 'POST',
        data: { style: style,
                kutije: kutije,
                linije: linije },
        dataType: "json"        
    })
    .done(function(reply) {
        //console.log(reply); 
        
        var datumfajl = reply.date;
        //var datum = reply;
        var arrdatum = datumfajl.split('-');
        datum = "<span>" + arrdatum[0] + "." + arrdatum[1]+".</span> - <span>"+arrdatum[3] + ":" + arrdatum[4]+"</span>";
        ver = '<span class="ver"><b>Verzija-'+count++ +'</b><date>'+datum+'</date><span class="uk-button uk-button-success ucitaj">Ucitaj</span><button class="uk-button uk-button-danger obrisi"  type="button">Obrisi</button><span class="loadverzija">'+datumfajl+'</span></span> ';
        $("#ver").append(ver);
        verNumbers = $('#ver .ver').length;
        btn = $('.verzije .snimi').get(0);
        
        $('.obrisi').on('click', function(event) {
            event.preventDefault();
            verzijaIme = $(this).parent().find('b').text();
            $(this).parent().remove();
            btn.removeAttribute("disabled","");
            verNumbers = $('#ver .ver').length;
            loadingon();
            brisifajl = $(this).parent().find('.loadverzija').text();
        });
                
        console.log(saveVer+": "+filename+".html");
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("file saved");
    });
}